package adventure.placeable.trigger.results;

import adventure.room.RoomManager;
import adventure.room.RoomManagerEdit;
import colour.ColourUtil;
import java.util.StringTokenizer;
import player.Player;
import server.util.Broadcaster;

/**
 *
 * @author Michael Hanns
 *
 */
public class ResultModifyRoom extends TriggerResult implements Cloneable {

    private String modifyWhat;
    private String strMod;
    private int intMod;

    public ResultModifyRoom(String result, String modWhat,
            int uses, String strMod) {
        super(result, uses);
        this.modifyWhat = modWhat;
        this.strMod = strMod;
        this.intMod = 0;
    }

    public ResultModifyRoom(String result, String modWhat, String strMod) {
        super(result);
        this.modifyWhat = modWhat;
        this.strMod = strMod;
        this.intMod = 0;
    }

    public ResultModifyRoom(String result, String modWhat,
            int uses, int intMod) {
        super(result, uses);
        this.modifyWhat = modWhat;
        this.strMod = "";
        this.intMod = intMod;
    }

    public ResultModifyRoom(String result, String modWhat, int intMod) {
        super(result);
        this.modifyWhat = modWhat;
        this.strMod = "";
        this.intMod = intMod;
    }

    public ResultModifyRoom() {
        super();
        this.modifyWhat = "";
        this.strMod = "";
        this.intMod = 0;
    }

    public void setModifyWhat(String mod) {
        modifyWhat = mod;
    }

    public void setStringMod(String mod) {
        strMod = mod;
    }

    public void setIntMod(int mod) {
        intMod = mod;
    }

    public String getModifyWhat() {
        return modifyWhat;
    }

    public String getStrMod() {
        return strMod;
    }

    public int getIntMod() {
        return intMod;
    }

    @Override
    public String getDescription() {
        if (super.limitedUses()) {
            String desc = super.resultText + ", uses: " + super.usesRemaining()
                    + " / MODROOM " + modifyWhat;
            return desc;
        } else {
            String desc = super.resultText + " / MODROOM " + modifyWhat;
            return desc;
        }
    }

    @Override
    public String resultImplementation(RoomManager r, Player p, String in, boolean previousFailed, Broadcaster out) {
        if (allConditionsMet(r, p, in, previousFailed)) {
            if (modifyWhat.equalsIgnoreCase("CURRENCY")) {
                ((RoomManagerEdit) r).editRoom(p.x(), p.y(), p.z(), modifyWhat, intMod);
            } else {
                ((RoomManagerEdit) r).editRoom(p.x(), p.y(), p.z(), modifyWhat, strMod);
            }

            useOnce();
            String output = replaceCurrency(replaceKeywords(replaceKeywords(p), r, p.x(), p.y(), p.z()), intMod);
            out.broadcastLocalToPlayer(p, replaceYou(output, p, true));

            return replaceCurrency(replaceYou(output, p, false), intMod);
        } else {
            return "";
        }
    }

    @Override
    public String getType() {
        return "MODIFYROOM";
    }

    private String replaceKeywords(String text, RoomManager r, int x, int y, int z) {
        StringTokenizer tokens = new StringTokenizer(text);
        String finalText = "";
        while (tokens.hasMoreTokens()) {
            String word = tokens.nextToken();
            if (word.equalsIgnoreCase("[TITLE]")) {
                word = ColourUtil.colourise(r.getRoomTitle(x, y, z), ColourUtil.roomTitle);
            } else if (word.equalsIgnoreCase("[DESCRIPTION]")) {
                word = ColourUtil.colourise(r.getRoomDescription(x, y, z), ColourUtil.roomDescription);
            } else if (word.equalsIgnoreCase("[USES]")) {
                word = ColourUtil.colourise(super.usesRemaining() + "", ColourUtil.value);
            }

            if (finalText.length() > 0) {
                finalText = finalText + " " + word;
            } else {
                finalText = word;
            }
        }
        return finalText;
    }

    @Override
    public String replaceKeywords(Player p) {
        return resultText.replace("[PLAYER]", ColourUtil.colourise(p.getName(), ColourUtil.player))
                .replace("[USES]", ColourUtil.colourise(super.usesRemaining() + "", ColourUtil.value));

    }
}
